<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/13
 * Time: 14:39
 */

namespace app\api\controller\common;
use app\api\model\ValidateCode as ValidateCodeModel;
use think\Cookie;

class ValidateCode
{
    static function createCode($email){ //定义一个生成验证码的函数
        //生成一个随机验证码
        $code = mt_rand(1000,9999);
        $vc = new ValidateCodeModel;
        //让当前用户之前的验证码所有的失效 ,在数据库中标记为失效
        $vc->where([
            'verify_sign'=>Cookie::get("verify_sign"),
            'email'=>$email,
            'status'=>0
        ])->update(['status'=>1]);

        //存储
        $vc->save([
            'email'=>$email,
            'code'=>$code,
            'verify_sign'=>Cookie::get("verify_sign")
        ]);
        return $code; //返回
    }
}